package edu.jmu.service.message.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import edu.jmu.bean.QueryResult;
import edu.jmu.bean.message.Message;
import edu.jmu.bean.message.MessageToUser;
import edu.jmu.bean.user.User;
import edu.jmu.service.base.DaoSupport;
import edu.jmu.service.message.MessageToUserService;
import edu.jmu.utils.WebUtil;
/***
 * 
 * @author Geven7
 *
 */
@Service
public class MessageToUserServiceBean extends DaoSupport<MessageToUser> implements
		MessageToUserService {
	Query query = null;
	
	@Override
	@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
	public QueryResult<MessageToUser> GetMessageToUserByMessageId(int messageId) {
		String wherejpql = " o.message.messageId = ?1";
		return super.getScrollData(0, Integer.MAX_VALUE, wherejpql, new Integer[]{messageId});
	}

	@Override
	@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
	public QueryResult<MessageToUser> GetAllMessageToUserByFromUser(User fromUser) {
		String wherejpql = " o.fromUser = ?1";
		return super.getScrollData(0, Integer.MAX_VALUE, wherejpql, new User[]{fromUser});
	}
	
	@Override
	@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
	public QueryResult<MessageToUser> GetMessageToUserByFromUser(User fromUser) {
		String wherejpql = " o.fromUser = ?1 and o.message.ifPublish = 1";
		return super.getScrollData(0, Integer.MAX_VALUE, wherejpql, new User[]{fromUser});
	}

	@Override
	@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
	public QueryResult<MessageToUser> GetMessageToUserByToUser(User toUser){
		String wherejpql = " o.toUser = ?1 and o.message.ifPublish = 1";
		return super.getScrollData(0, Integer.MAX_VALUE, wherejpql, new User[]{toUser});
	}

	@Override
	@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
	public int GetMessageToUserCountByToUserName(User toUser) {
		String wherejpql = "(o.toUser = ?1 or o.toUser is null) and (o.fromUser != ?2) and (o.message.ifPublish = 1)";
		int count  = super.getScrollData(0,Integer.MAX_VALUE,wherejpql,new User[]{WebUtil.getUser(),WebUtil.getUser()}).getResultlist().size();
		return count;
	}

	@Override
	@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
	public int GetMessageToUserUnreadCountByToUserName(User toUser) {
		String wherejpql = "(o.toUser = ?1 or o.toUser is null) and (o.fromUser != ?2) and (o.message.ifPublish = 1) and  (o.isRead = 0)";
		int count  = super.getScrollData(0,Integer.MAX_VALUE,wherejpql,new User[]{WebUtil.getUser(),WebUtil.getUser()}).getResultlist().size();
		return count;
	}

	@Override
	@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
	public int GetMessageToUserCountByFromUserName(User fromUser) {
		String wherejpql = "o.fromUser = ?1 and o.message.ifPublish = 1";
		int count  = super.getScrollData(0,Integer.MAX_VALUE,wherejpql,new User[]{WebUtil.getUser()}).getResultlist().size();
		return count;
	}
	
	@Override
	public void DeleteMessageToUserByMessageId(int messageId) {
		String jpql = "delete from MessageToUser o where o.message.id = ?1";
		em.createQuery(jpql).setParameter(1, messageId).executeUpdate();
	}
	
	public void publishMessage(Message message){
		String today;
		Date dt = new Date();
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    today = sdf.format(dt);
		String jpql = "update Message as o set o.ifPublish = 1 ,o.recordTime = ?1 where o.id=?2";
		try {
			em.createQuery(jpql).setParameter(1, sdf.parse(today)).setParameter(2, message.getId()).executeUpdate();
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	/*@Override
	public boolean SaveMessageToUser(List<MessageToUser> messageToUserList) {
		boolean valid =  false;
		if(messageToUserList.size() > 0 && messageToUserList != null)
		{
			for(MessageToUser messageToUser : messageToUserList)
			{
				this.save(messageToUser);
				valid = true;
			}
		}
		return valid;
	}*/

}
